#ifndef FK_FACTORY
#define FK_FACTORY

#include <QMap>
#include <QString>

template <class D>
class FKAbstractCreator{
public:
    virtual ~FKAbstractCreator(){}
    virtual D* create()=0;
    virtual D* create(qint32 id)=0;
};

template <class D, class F>
class FKCreator:public FKAbstractCreator<D>{
public:
    D* create(){return new F;}
    D* create(qint32 id){return new F(0,id);}
};

template <class D>
class FKFactory{
public:
    ~FKFactory(){
        foreach(FKAbstractCreator<D>* creator,_classes)delete creator;
    }
    template <class F>
    void addClass(){
        QString className(F::staticMetaObject.className());
        if(!_classes.contains(className)){
            _classes[className]=new FKCreator<D,F>();
        }
    }
    D* createObject(QString className){
        if(_classes.contains(className)){
            return _classes.value(className)->create();
        }else{
            return 0;
        }
    }
    D* createObject(QString className, qint32 id){
        if(_classes.contains(className)){
            return _classes.value(className)->create(id);
        }else{
            return 0;
        }
    }
private:
    QMap<QString,FKAbstractCreator<D> * > _classes;
};

#endif
